/**
 * 规则库初始化
 */
var SdsRuleBase = {
    id: "SdsRuleBaseTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
};
SdsRuleBase.refresh = function () {
    SdsRuleBase.table.refresh();
}

/**
 * 初始化表格的列
 */
SdsRuleBase.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '规则代码', field: 'ruleCode', align: 'center', width: '250px', valign: 'middle', sortable: false},
        {title: '规则名称', field: 'ruleDesc', align: 'center', width: '350px', valign: 'middle', sortable: false},
        {title: '备注', field: 'comment', align: 'center',width:'400px', valign: 'middle', sortable: false},
        {title: '默认值', field: 'isDefault', align: 'center', width: '250px',valign: 'middle', sortable: false,formatter:function (value, row, index){
                if (value == 1) {
                    return "<span class='label label-success'>true</span>";
                } else {
                    return "<span class='label label-warning'>false</span>";
                }
            }},
        {title: '计算类型', field: 'calType', align: 'center', valign: 'middle', sortable: false,},
        {title: '操作', field: 'id', align: 'center', width: '150px', valign: 'middle',formatter:function(value,row,index){
                var _html = '<a href="javascript:SdsRuleBase.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
                _html += '<a href="javascript:SdsRuleBase.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="hisui-icon icon-cancel"></i></a>';
                return _html;
            }}];
};

/**
 * 检查是否选中
 */
SdsRuleBase.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsRuleBase.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增
 */
SdsRuleBase.add = function () {
    var index = Sys.open('新增规则',Sys.backendPath + '/sds/sdsstandrulebase/to_add',['800px', '600px']);
    this.layerIndex = index;
};

/**
 * 跳转修改规则
 */
SdsRuleBase.edit = function (id) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        this.layerIndex = Sys.open('修改规则',Sys.backendPath + '/sds/sdsstandrulebase/to_update/' + id,['800px', '600px']);
    }
};

/**
 * 删除数据源
 */
SdsRuleBase.del = function (id,name) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
    if (flag) {
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrulebase/delete", function (data) {
                Sys.success("删除成功!");
                SdsRuleBase.table.refresh();
            }, function (data) {
                Sys.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("SdsRuleBaseId",id);
            ajax.start();
        };
        Sys.confirm("是否删除规则?",operation);
    }
};

/**
 * 查询数据源
 */
SdsRuleBase.search = function () {
    var queryData = {};
    queryData['ruleCode'] = $("#code").val();
    queryData['ruleDesc'] = $("#condition").val();
    SdsRuleBase.table.refresh({query: queryData});
};


$(function () {

    var defaultColunms = SdsRuleBase.initColumn();
    var table = new BSTable(SdsRuleBase.id, Sys.backendPath + "/sds/sdsstandrulebase/list", defaultColunms);
    table.setPaginationType("server");
    table.setSubHeight(-10);
    table.setPageSize(15);
    // table.setSort("weight");
    table.init();
    SdsRuleBase.table = table;

});
